package com.liangzhong.modules.task.mediator
{
	import com.common.queueloader.QueueLoaderEvent;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.CompeteEvent;
	import com.liangzhong.events.TaskEvent;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.mediator.ModuleMediator;
	import com.liangzhong.model.AppData;
	import com.liangzhong.proxyconstant.compete.CompeteProxyConstant;
	import com.liangzhong.proxyconstant.task.TaskProxyConstant;
	import com.lz.football.module.base.IComponent;
	import com.lz.football.module.base.IModuleView;
	import com.lz.football.module.task.ITask;
	import com.lz.football.module.task.ITaskFinishView;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	
	import lz.controls.managers.AnimationManager;
	import lz.controls.managers.PopUpManager;
	
	import org.puremvc.as3.interfaces.INotification;
	
	public class TaskMediator extends ModuleMediator
	{
		public static const MediatorName :String = "TaskMediator";
		public function TaskMediator(mediatorName:String=null, viewComponent:Object=null, param:Object=null)
		{
			super(mediatorName, viewComponent, param);
		}
		
		/**
		 *获取 实例 
		 * @return 
		 * 
		 */		
		public function get clubView():DisplayObjectContainer
		{
			return viewComponent as DisplayObjectContainer;
		}
		/**
		 *添加事件 
		 * 
		 */		
		override public function addEventListeners():void
		{
			AppEventDispatcher.getInstance().addEventListener(TaskEvent.GET_TASK_FINISH_WARD,onTaskfinishward);
			AppEventDispatcher.getInstance().addEventListener(CompeteEvent.DONATE_FROM_TASK,onDonateFromTaskfinish);
		}
		
		protected function onDonateFromTaskfinish(event:CompeteEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(CompeteProxyConstant.COMPETE_MEDIATORTOCOMMAND,event.param,CompeteProxyConstant.DONATE);
		}
		
		protected function onTaskfinishward(event:TaskEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(TaskProxyConstant.TASKMEDTOCOMMAND,event.param,TaskProxyConstant.GET_TASK_WARD);
		}
		/**
		 * 删除事件 
		 * 
		 */
		override public function removeEventListeners():void
		{
			AppEventDispatcher.getInstance().removeEventListener(TaskEvent.GET_TASK_FINISH_WARD,onTaskfinishward);
		}
		/**
		 * 监听信息
		 * @return 
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [CompeteProxyConstant.GETDONATE].concat(super.listNotificationInterests());
		}
		/**
		 * 处理信息
		 * @param notification
		 * 
		 */		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case CompeteProxyConstant.GETDONATE:
					(clubView as ITask).updateDonate();
					break;
					
					
				
			}
		}
		/**
		 * 
		 * 注册
		 */		
		override public function onRegister():void
		{
			showUI();
//			resName = ResourceConst.TASK_SWF;
//			if(ResourceManager.instance.isContainResource(ResourceConst.TASK_SWF))
//			{
//				showUI();
//			}
//			else
//			{
//				loader = new QueueLoader();
//				var clubConfig :XML = XML(ResourceManager.instance.getResource(ResourceConst.MODULE_RES_CONFIG)).club[0];
//				var releaseVersion :String;
//				for each(var item :XML in clubConfig.item)
//				{
//					releaseVersion = "?" + item.@version + "." + item.@itemType;
//					loader.addQueue(new QueueVo(item.@resName,item.@id,item.@id),item.@url + releaseVersion);
//				}
//				loadRes();
//			}
		}
		/**
		 *加载资源完毕 
		 * @param e
		 * 
		 */		
		override protected function onLoadComplete(e:QueueLoaderEvent):void
		{
			showUI();
			super.onLoadComplete(e);
			ResourceManager.instance.putResource(ResourceConst.CLUB_RESOURCE,resList)
		}
		private function showUI():void
		{
			var cView :IComponent = Reflection.createInstance("com.lz.football.module.task.view.TaskView");
			viewComponent = cView;
			addEventListeners();
			cView.initUI();
			
			GlobalDef.GameWindowContainer.addChild(viewComponent as DisplayObject);
			
		}
		/**
		 * 删除 mediator
		 * 
		 */		
		override public function removeMediator():void
		{
			facade.removeMediator(MediatorName);
		}
		/**
		 *删除 清理 
		 * 
		 */		
		override public function onRemove():void
		{
			super.onRemove();
			removeEventListeners();
			(viewComponent as IModuleView).destroyView();
			viewComponent = null;
		}
	}
}